<?php 
/**
 * 
 * 课程安排类
 *
 */

defined('IN_PHPCMS') or exit('No permission resources.');

class courses_tag {
	private $db;
	private $classes;
	private $courses;
	
	public function __construct() {
		$this->db = pc_base::load_model('courses_model');
	}
	
	/**
	 * 课时安排列表方法
	 * @param array $data 传递过来的参数
	 * @param return array 数据库中取出的数据数组
	 */
	public function lists($data) {
		/*
		 * Array ( [startdate] => 2011-03-30 [enddate] => 2011-03-30 [order] => ctime ASC [limit] => 2 ) 
		 */
		$return = array();
		$where = '1';
		$siteid = $data['siteid'] ? intval($data['siteid']) : get_siteid();
		if ($siteid) $where .= " AND `siteid`='".$siteid."'";
		$data['startdate'] = trim(strip_tags($data['startdate']));
		$data['startdate'] = strtotime($data['startdate']." 00:00:00");
		$data['startdate'] = $data['startdate']>0 ? $data['startdate'] : strtotime(date('Y-m-d', SYS_TIME)." 00:00:00");
		$data['enddate'] = trim(strip_tags($data['enddate']));
		$data['enddate'] = strtotime($data['enddate']." 23:59:59");
		$data['enddate'] = $data['enddate']>0 ? $data['enddate'] : strtotime(date('Y-m-d', SYS_TIME)." 23:59:59");
		
		$this->courses = $this->get_available_courses($siteid);
		$this->classes = $this->get_available_classes($siteid);
		if (empty($this->courses) || empty($this->classes)) return array();
		$available_classes_str = implode(',', array_keys($this->classes));
		$available_courses_str = implode(',', array_keys($this->courses));
		
		$where .= " AND `csid` IN ($available_courses_str) ";
		$where .= " AND `cid` IN ($available_classes_str) ";
		$where .= " AND `ctime`>='".$data['startdate']."' ";
		$where .= " AND `ctime`<='".$data['enddate']."' ";
		$where .= ' AND `ctused`=\'1\'';
		$query = $this->db->select($where, '*', $data['limit'], $data['order']);
		if ($query)
		{
			foreach ($query as $key=>$val)
			{
				$val['cname'] = $this->classes[$val['cid']];
				$val['csname'] = $this->courses[$val['csid']];
				$val['cstime'] = date("H:i", $val['ctime']);
				$return[] = $val;
			}
		}
		return $return;
	}
	
	/**
	 * 获得可用的课程
	 */
	public function get_available_courses($siteid)
	{
		$return = array();
		$this->db->set_model(2);
		$query = $this->db->select(array('siteid'=>$siteid, 'csused'=>"1"));
		if ($query)
		{
			foreach ($query as $key=>$val)
			{
				$return[$val['csid']] = $val['csname'];
			}
		}
		$this->db->set_model();
		return $return;
	}	
	
	/**
	 * 获得可用的班级列表
	 */
	public function get_available_classes($siteid)
	{
		$return = array();
		$this->db->set_model(1);
		$query = $this->db->select(array('siteid'=>$siteid, 'cused'=>"1"));
		if ($query)
		{
			foreach ($query as $key=>$val)
			{
				$return[$val['cid']] = $val['cname'];
			}
		}
		$this->db->set_model();
		return $return;
	}
	
	public function count() {
		
	}
	
	/**
	 * pc标签初始方法
	 */
	public function pc_tag() {
		//获取站点
		$sites = pc_base::load_app_class('sites','admin');
		$sitelist = $sites->pc_tag_list();
		$result = getcache('special', 'commons');
		if(is_array($result)) {
			$specials = array(L('please_select', '', 'announce'));
			foreach($result as $r) {
				if($r['siteid']!=get_siteid()) continue;
				$specials[$r['id']] = $r['title'];
			}
		}
		return array(
			'action'=>array('lists'=>L('lists', '', 'announce')),
			'lists'=>array(
				'siteid'=>array('name'=>L('sitename', '', 'announce'),'htmltype'=>'input_select', 'defaultvalue'=>get_siteid(), 'data'=>$sitelist),
			),
		);
	}
}
?>